package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import edu.chinasoft.pojo.User;
import edu.chinasoft.service.UserService;
import edu.chinasoft.service.UserServiceImp;

/**
 *
 * @author mhao
 * @date 2020年2月12日:下午3:56:48
 * @version V1.0
 * @Description: 
*/
@WebServlet(name = "myCheckUserNameForServlet", value = "/servlet/checkusernameforservlet")
public class CheckUserNameForServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(CheckUserNameForServlet.class);
	private UserService userService = new UserServiceImp();
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" CheckUserNameForServlet >doGet start.......");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		//解决乱码
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		response.setHeader("expires", "0");
		String userName = request.getParameter("username");
		userName = URLDecoder.decode(userName,"utf-8");
		String result="";
		//采用四层架构，调用服务层的方法判断该用户是否存在
		User user = new User();
		user.setUserDesc(userName);
		boolean resultSql=userService.selectUserByName(user);
		if(resultSql) {
			result="exists";
		}else {
			result="no exists";
		}
		response.getWriter().write(result);
		logger.debug(" CheckUserNameForServlet >doGet end.......");
	}

	/**
	 *
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" CheckUserNameForServlet >doPost start.......");
		//解决乱码
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		response.setHeader("expires", "0");
		String userName = request.getParameter("username");
		System.out.println(userName);
		String result="";
		//采用四层架构，调用服务层的方法判断该用户是否存在
		User user = new User();
		user.setUserName(userName);
		boolean resultSql = userService.selectUserByName(user);
		if(resultSql) {
			result="exists";
		}else {
			result="no exists";
		}
		response.getWriter().write(result);
		logger.debug(" CheckUserNameForServlet >doPost end.......");
	}

}